/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.manto;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.manto.PantallaForm;
import com.uca.sisbec.helpers.manto.PantallaHelper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

/**
 *
 * @author Juank
 */
public class PantallaAction extends HibernateAction {
    
    public ActionForward init(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setAttribute("save", "new");
        setOptionsList(request);
        return mapping.findForward("success");
    }

    public ActionForward initBusqueda(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        return mapping.findForward("search");
    }
    
    public ActionForward agregarPantalla(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        PantallaForm pf = (PantallaForm) form;
        PantallaHelper helper = new PantallaHelper(getSession(request));
        ActionMessages msg = new ActionMessages();
        int codigo = helper.insertarPantalla(pf);
        pf.setCodigo(codigo);
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("pantalla.nuevo"));
        request.setAttribute("msg", msg);
        request.setAttribute("save", "update");
        setOptionsList(request);
        return mapping.findForward("success");
    }
    
    public ActionForward modificarPantalla(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        PantallaForm pf = (PantallaForm) form;
        PantallaHelper helper = new PantallaHelper(getSession(request));
        ActionMessages msg = new ActionMessages();
        helper.modificarPantalla(pf);
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("pantalla.modificado"));
        request.setAttribute("msg", msg);
        request.setAttribute("save", "update");
        setOptionsList(request);
        return mapping.findForward("success");
    }

    public ActionForward buscarPantallas(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        PantallaForm pf = (PantallaForm) form;
        PantallaHelper helper = new PantallaHelper(getSession(request));
        request.setAttribute("getPantallasList", helper.getPantallasList(pf));
        return mapping.findForward("search");
    }

    public ActionForward seleccionarPantalla(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        PantallaForm pf = (PantallaForm) form;
        PantallaHelper helper = new PantallaHelper(getSession(request));
        helper.getPantallaForm(pf);
        request.setAttribute("save", "update");
        setOptionsList(request);
        return mapping.findForward("success");
    }

    private void setOptionsList(HttpServletRequest request) {
        PantallaHelper helper = new PantallaHelper(getSession(request));
        request.setAttribute("parentsList", helper.getMenuPrincipalList());
    }
}
